
class JsonConfigFile{
    constructor(path, default_data){
        this.path = path;
        if (fs.existsSync(path) == false){
            fs.mkdirSync(path.split("/").slice(0, -1).join("/"), {recursive: true});
            fs.writeFileSync(path, default_data);
            this.json_str = fs.readFileSync(path);
        }else{
            this.json_str = fs.readFileSync(path);
        }
    }

    read(){
        return this.json_str;
    }

    readAllParse(){
        let res;
        try{
            res = JSON.parse(this.json_str);
        }catch(err) {
            res = null;
            logger.error(err);
        }
        return res;
    }

    writeAll(data){
        if (typeof(data) == 'string'){
            return fs.writeFileSync(this.path, data);
        }else{
            return fs.writeFileSync(this.path, JSON.stringify(data, null, 4));
        }
    }
}

module.exports = {
    JsonConfigFile
};